<?php  
 class loginsuccess extends CI_Controller {
	 private $data;
  function loginsuccess()
    {
	  global $data;
     parent::__construct();
     $this->load->model('login_model');
       $this->load->helper("url_helper");
	   	   $this->load->model('common_model');
 
	$result=$this->common_model->getKeywords('login'); 
	$data['keywords']=$result;
       session_start();

    }
 
	public function index()
	     {
		 global $data;
		if (isset($_SESSION['logged_in']))
			 {
			
			 $course_details=$this->login_model->getcourse();
			 $question_details=$this->login_model->getallquestions();

	    $data['course_details'] = '';               
        $data['question_details'] = '';
		 
		 $usertypeId=($_SESSION['rollId']);
		 if($usertypeId=="1")
			  {

        if($course_details!=false)
		  {
        $data['course_details']=$course_details;
		  }
		  if($question_details!=false)
		  {
        $data['question_details']=$question_details;
		  }
		  $this->load->view('addquestscreen',$data);
		  }
		  else if($usertypeId=="2")
			  {

			  if($course_details!=false)
		       {
        $data['course_details']=$course_details;
		        }
		 $this->load->view('emplogin',$data);
		  }
          else if($usertypeId=="3")
			  {
         $assgnmets=$this->loaduserassignment();
		 $data['user_asignments']="";
		 if($assgnmets!=false)
				  {

			$data['user_asignments']=$assgnmets;
				  }
		 $this->load->view('userlogin',$data);
		  }
		  }
        else{
		  redirect(MAIN_URL."index.php/login");
       }

		 }

 public function insertquestion()
	    {
	  global $data;
	    

		 if (isset($_SESSION['logged_in']))
			 {
		 
        $quest=$_POST['question'];
        $answer=$_POST['answer'];
	    $course_id=$_POST['course_combo'];
        $userId=$_SESSION['logged_in'];

	    $insertQuestion=$this->login_model->insertquestion($course_id, $quest,$answer,$userId);
       if($insertQuestion=="1")
			 {
		   $this->session->set_flashdata('question_response', TRUE);
			 }
		    redirect('loginsuccess');
		   
		     
			  
			 }

		}

 public function insertstudent()
	    {
	     global $data;

		 if (isset($_SESSION['logged_in']))
			 {
		 
        $name=$_POST['stuname'];
        $contactno=$_POST['cnumber'];
	    $email=$_POST['emailid'];
		$address=="";
		if($_POST['address']){
        $address=$_POST['address'];
			 }
        $courseid=$_POST['course_combo'];
        $password=$_POST['password'];

	    $insertstudent=$this->login_model->insertstudent($name, $contactno,$email,$address,$courseid,$password);
       if($insertstudent=="1")
			 {
		   $this->session->set_flashdata('student_response', TRUE);
			 }
		    redirect(MAIN_URL."index.php/loginsuccess/adduser");
		   
		     
			  
			 }

		}

		public function insertmultiplequestion()
	    {
	     global $data;

		 if (isset($_SESSION['logged_in']))
			 {
		 
        $quest=$_POST['question'];
        $answer=$_POST['answer'];
		$opt1=$_POST['opt1'];
		$opt2=$_POST['opt2'];
		$opt3=$_POST['opt3'];
		$opt4=$_POST['opt4'];
		$opt5=$_POST['opt5'];
		 
	    $course_id=$_POST['course_combo'];
        $chapterno="";
		if($_POST['chapternames'])
				 {
		$chapterno=$_POST['chapternames'];
				 }
        $userId=$_SESSION['logged_in'];
		$muliplechoice=$_POST['option_multi'];

	    $insertQuestion=$this->login_model->insertmultiplequestion($course_id, $quest,$answer,$userId,$opt1,$opt2,$opt3,$opt4,$opt5,$chapterno,$muliplechoice);
       if($insertQuestion=="1")
			 {
		   $this->session->set_flashdata('question_response', TRUE);
			 }

			 redirect(MAIN_URL."index.php/loginsuccess/addmultiquestion");
		   
			 }

		}



public function logout()
	    {
	 global $data;
           if (isset($_SESSION['logged_in']))
			{
			  unset($_SESSION['logged_in']);
			}
			if (isset($_SESSION['rollId']))
			{
				unset($_SESSION['rollId']);
			}
			if (isset($_SESSION['name']))
			{
				unset($_SESSION['name']);
			}
         redirect(MAIN_URL);
		}





function actionPerform()
	{
 global $data;
if (isset($_SESSION['logged_in']))
			 {
		  $userId=$_SESSION['logged_in'];
         if($_POST['actionPerform'])
				 {
          $actionPerform=$_POST['actionPerform'];
		 
          if($actionPerform=="ChangePassword")
					 {
           $this->load->view('manageaccountscreen',$data);
		  
			 }


         else if($actionPerform=="Logout")
		   {

          $this->logout();
		  
			 }
			 else {
           redirect(MAIN_URL."index.php/loginsuccess");
			 }


			 }

			 else {
           redirect(MAIN_URL."index.php/loginsuccess");
			 }
		}

            else
		       {
        redirect(MAIN_URL."index.php/login");

            }





	}












function manageaccount()
	{
         if (isset($_SESSION['logged_in']))
			 {
		  $userId=$_SESSION['logged_in'];
          $this->load->view('manageaccountscreen',$data);
		  
			 }

			 else
		       {
        redirect(MAIN_URL."index.php/login");

            }
      }




	function updatepassword()
	{
         if (isset($_SESSION['logged_in']))
			 {
		   

          $userId=$_SESSION['logged_in'];
		  $uname=$_SESSION['name'];
	 
		  $oldpassword=$_POST['oldpass'];
          $newpassword=$_POST['newpass'];
          $updatepassresult=$this->login_model->updatepassword($userId,$oldpassword,$newpassword,$uname);
         
		 $this->session->set_flashdata('password_response', $updatepassresult);
		      
        redirect(MAIN_URL."index.php/loginsuccess/manageaccount");
		   }
			     

			 else
		       {
        redirect(MAIN_URL."index.php/login");

            }
      }

function adduser()
	
{
$course_details=$this->login_model->getcourse();
$data['course_details'] = '';  
if($course_details!=false)
		{
             
$data['course_details']=$course_details;
		}
$this->load->view('addstudentscreen',$data);
}

function addcourse()
	
{


$this->load->view('addcoursescreen');

}

function craeteassignment()
	
      {
        $course_details=$this->login_model->getcourse();
		 

	    $data['course_details'] = '';               
        $data['question_details'] = '';
		 if($course_details!=false)
		  {
        $data['course_details']=$course_details;
		  }
		   
		   

$this->load->view('assignmentscreen',$data);

}

function assignassignment()
	
      {
       $course_details=$this->login_model->getcourse();
		 

	    $data['course_details'] = '';               
         
		 if($course_details!=false)
		  {
        $data['course_details']=$course_details;
		  }
		   
		   

$this->load->view('assignassignmentmentscreen',$data);

}



function addmultiquestion()
	
{
$course_details=$this->login_model->getcourse();
$data['course_details'] = '';               
$data['course_details']=$course_details;
$this->load->view('insertmultiquestscreen',$data);
		 }


function updatequestion()
	
{
$course_details=$this->login_model->getcourse();
$question_details=$this->login_model->getallquestions();
$data['course_details'] = '';               
$data['course_details']=$course_details;
$data['question_details'] = ''; 
$data['question_details']=$question_details;
$this->load->view('updatequestscreen',$data);
		   

		  }


public function insertcourse()
	    {
	    $coursename=$_POST['coursename'];
        $description=$_POST['description'];
	     

	   $insertQuestion=$this->login_model->insertcourse($coursename,$description);
       if($insertQuestion=="1")
			 {
		   $this->session->set_flashdata('course_response', TRUE);
			 }
        redirect(MAIN_URL."index.php/loginsuccess/addcourse");
		   }


public function loadsyllabus()
	{
$courseid=$_POST['courseid'];
$coursechapters=$this->login_model->getcoursechapters($courseid);
 if($coursechapters!=false)
		{
	echo "Select Chapter :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <select id='chapternames' name='chapternames'>";
 echo "<option value='-1' selected> ";
  
echo "--Select Chapter--";
foreach ($coursechapters->result() as $row)
            {
	 echo"<option value=\"{$row->chapterno}\">{$row->name}</option>";
     echo"<br>";
      }
	  echo" </select>";
	}

	else
		{
		echo "No Result found";
		}

}

public function loadsyllabuschange()
	{
$courseid=$_POST['courseid'];
$coursechapters=$this->login_model->getcoursechapters($courseid);
 
 if($coursechapters!=false)
		{
	echo "Select Chapter :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <select id='chapternamesv' name='chapternames' onchange='loadcoursequestion()'>";
 echo "<option value='-1' selected> ";
  
echo "--Select Chapter--";
foreach ($coursechapters->result() as $row)
            {
	 echo"<option value=\"{$row->chapterno}\">{$row->name}</option>";
     echo"<br>";
      }
	  echo "<option value=100>All</option>";
	  echo" </select>";
	}

	else
		{
		echo "No Result found";
		}

}



public function loadsyllabuschangeandstudents()
	{
$courseid=$_POST['courseid'];
$coursechapters=$this->login_model->getcoursechapters($courseid);
 
 if($coursechapters!=false)
		{
	echo "Select Chapter :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <select id='asschapternames' name='asschapternames' onchange='loadassignments()'>";
 echo "<option value='-1' selected> ";
  
echo "--Select Chapter--";
foreach ($coursechapters->result() as $row)
            {
	 echo"<option value=\"{$row->chapterno}\">{$row->name}</option>";
     echo"<br>";
      }
	  echo "<option value=100>All</option>";
	  echo" </select>";
	}

	else
		{
		echo "No Result found";
		}
echo '||';
$studentslist=$this->login_model->getstudent($courseid);
 
 if($studentslist!=false)
		{
	echo "Select Chapter :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <select id='studentlistcombo' name='studentlistcombo' multiple='multiple'>";
 echo "<option value='-1' selected> ";
  
echo "--Select Chapter--";
foreach ($studentslist->result() as $row)
            {
	 echo"<option value=\"{$row->id}\">{$row->name}</option>";
     echo"<br>";
      }
	  echo "<option value=100>All</option>";
	  echo" </select>";
	}

	else
		{
		echo "No Result found";
		}


}

function loadassignments()
	{
    $courseid=$_POST['courseid'];
	$chapterno=$_POST['chapterno'];
	$multiplechoice=$_POST['multiplechoice'];
          
    $assignments = $this->login_model->loadassignments($courseid,$chapterno,$multiplechoice);

	  if($assignments!=false)
		  {
  
echo'<table id="insured_list" class="tablesorter"><thead><tr> 
        
	        <th>Assignment Id</th>
            <th>Name</th>
            <th>Description</th>
            <th>Question No</th>
            <th>Assign</th>
		</tr></thead><tbody>';
 
 
foreach ($assignments->result() as $row)
            {
	 echo '<tr>
	        <td>'.$row->assignmentid.'</td>
            <td>'.$row->name.'</td>
			<td>'.$row->description.'</td>
            <td>'.$row->questionno.'</td> 
			 <td>
			 <div id="remove'.$row->assignmentid.'" style="display:none">
						<input type="button" href="#" return="false" onclick=updateAssignmentStrings("'.$row->assignmentid.'","remove'.$row->assignmentid.'","add'.$row->assignmentid.'","removeassignment"); value=Remove >
					</div>

					<div id="add'.$row->assignmentid.'">
						<input type="button" href="#" return="false" onclick=updateAssignmentStrings("'.$row->assignmentid.'","add'.$row->assignmentid.'","remove'.$row->assignmentid.'","addassignment"); value=Add >
					</div>
			 
			 
			 
			 </td>
         </tr>';
		 
	  }
	  echo'</tbody></table>';
	  }
	 
	  else
		  {
		  echo"<b><h2>No Assignment Found</h2></b>";
		  }
 
 }

	 


public function insertassignment()
	{
    $userId=$_SESSION['logged_in'];
    $courseid=$_POST['courseid'];
	$chapterno=$_POST['chapterid'];
	$assignname=$_POST['assgname'];
	$assgdesc=$_POST['assgdesc'];
	$optionmultiple=$_POST['optionmultiple'];
    $questionarray=$_POST['questionarray'];
$response=$this->login_model->insertassignment($courseid,$chapterno,$assignname,$assgdesc,$questionarray,$userId,$optionmultiple);

if($response=="1")
		{
	echo "assignment added";
		}

		else{
		echo "Some error occurs please try later";
		}

	}


public function assignassignmenttostudents()
	{

    $studentId=$_POST['studentid'];
     
	$assignments=$_POST['assignments'];
	 
	
$response=$this->login_model->assignassignmenttostudents($studentId,$assignments);

if($response=="1")
		{
	echo "assignment added";
		}

		else{
		echo "Some error occurs please try later";
		}

	}



function userassignment()
	{
	if (isset($_SESSION['logged_in']))
			 {
			$data['user_asignments'] = '';
           $studentId=($_SESSION['logged_in']);
		   $assignments=$this->login_model->getuserassignments($studentId);
		   if($assignments!=false)
				 {
              $data['user_asignments'] = $assignments;
			 

				 }
				 $this->load->view('userassignmentscreen',$data);

	        }
else{
  redirect(MAIN_URL."index.php/login");
     }


	}
function loaduserassignment()
	{
	if (isset($_SESSION['logged_in']))
			 {
			$data['user_asignments'] = '';
           $studentId=($_SESSION['logged_in']);
		  $assignments=$this->login_model->getuserassignments($studentId);
		  if($assignments!=false)
				 {
              return $assignments;
			 

				 }
				 

	        }
else{
  return false;
     }


	}


public function loadcoursequestionandstudent()
	  {
	$courseid=$_POST['courseid'];
	$chapterno=$_POST['chapterno'];
	$multiplechoice=$_POST['multiplechoice'];
          
       $questans = $this->login_model->loadcoursequestion($courseid,$chapterno,$multiplechoice);
	   
      if($questans!=false)
		  {
		 
$QuestNo=0; 

echo'<table id="insured_list" class="tablesorter"><thead><tr> 
        
	        <th>S.No</th>
            <th>Course Name</th>
            <th>Chapter Name</th>
            <th>Question</th>
            <th>Option 1</th>
            <th>Option 2</th>
            <th>Option 3</th>
			<th>Option 4</th>
			<th>Option 5</th>
			<th>Assign</th>
		</tr></thead><tbody>';
 
 
foreach ($questans->result() as $row)
            {
	  echo '<tr>
	        <td>'.$row->id.'</td>
            <td>'.$row->courseName.'</td>
			<td>'.$row->name.'</td>
            <td>'.$row->question.'</td>
            <td>'.$row->opt1.'</td>
			<td>'.$row->opt2.'</td>
			<td>'.$row->opt3.'</td>
			<td>'.$row->opt4.'</td>
			<td>'.$row->opt5.'</td>
			 
			 <td>
			 <div id="remove'.$row->id.'" style="display:none">
						<input type="button" href="#" return="false" onclick=updateQuestionsStrings("'.$row->id.'","remove'.$row->id.'","add'.$row->id.'","removequestion",'.$QuestNo.'); value=Remove >
					</div>

					<div id="add'.$row->id.'">
						<input type="button" href="#" return="false" onclick=updateQuestionsStrings("'.$row->id.'","add'.$row->id.'","remove'.$row->id.'","addquestion",'.$QuestNo.'); value=Add >
					</div>
			 
			 
			 </td>
         </tr>';
		 $QuestNo++;
		 
	  }
	  echo'</tbody></table>';
	  }
	 
	  else
		  {
		  echo"<b><h2>No Questions Found</h2></b>";
		  }
 
	  }


function viewassquestions()
	{
	 
	if($_POST['assignmentid']){
	$assgnmentId=$_POST['assignmentid'];
	$assignmentrowid=$_POST['assignmentrowid'];

    $assignmentquestans = $this->login_model->getassignmentquestion($assgnmentId,$assignmentrowid);
   
	if($assignmentquestans!=false)
		{
      $data['assignmentquest']=$assignmentquestans;
      $data['assignmentid']=$assgnmentId;
	  $data['assignmentrowid']=$assignmentrowid;
	  $data['userid']=$_SESSION['logged_in'];
	  $this->load->view('assignmentquestionscreen',$data);
		}
else{
 redirect(MAIN_URL."index.php/loginsuccess/userassignment");
	}

	}

	else{
 redirect(MAIN_URL."index.php/loginsuccess/userassignment");
	}


	}

function submitattemptedassignment()
	{
	 
	 $assignmetid=$_POST['assignmtid'];
	 $questionno=$_POST['quest'];
	 $answer=$_POST['ans'];
    $assignmentquestans = $this->login_model->submitattemptedassignment($assignmetid,$questionno,$answer);
	 

	}



function assignmentcurrentstatus()
	{
	
	 $assignmetid=$_POST['assignmtid'];
	 $questionno=$_POST['quest'];
	 $answer=$_POST['ans'];
	 $assignmentquestans = $this->login_model->assignmentcurrentstatus($assignmetid,$questionno,$answer);
	 
	}



function userassignmentresult()
	{
		if (isset($_SESSION['logged_in']))
			 {
			$userid=$_SESSION['logged_in'];

 $assignmentquestans = $this->login_model->getassignmentresult($userid);
 
			$data['assgnmet_result'] = '';
           if($assignmentquestans!=false)
				 {
              $data['assgnmet_result'] = $assignmentquestans;
			  }
				 $this->load->view('assignmentresultscreen',$data);

	        }
else{
  redirect(MAIN_URL."index.php/login");
     }

 


	}












}